package com.qf.day4.huihua;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @date 2022/3/28
 * @desc
 */
@WebServlet("/huihua2")
public class HuiHua2Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=UTF-8");


        //String message = (String) req.getAttribute("message");
        //
        //req.setAttribute("message",message);

        //String message = (String) req.getServletContext().getAttribute("message");

        //this.getServletContext().setAttribute("message",message);

        Cookie[] cookies = req.getCookies();

        for (int i=0;cookies!=null&&i<cookies.length;i++){
            if(cookies[i].getName().equals("product")){
                String value = cookies[i].getValue();
                req.setAttribute("message",value);
            }
        }


        req.getRequestDispatcher("/day4/huihua/pay.jsp").forward(req,resp);
    }
}
